package com.byyy.ccts.modules.llcx.service.impl;

import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.modules.llcx.entity.LlcxBaseUser;
import com.byyy.ccts.modules.llcx.entity.LlcxDanw;
import com.byyy.ccts.modules.llcx.entity.LlcxRoleUser;
import com.byyy.ccts.modules.llcx.entity.LlcxUser;
import com.byyy.ccts.modules.llcx.mapper.LlcxBaseUserMapper;
import com.byyy.ccts.modules.llcx.mapper.LlcxDanwMapper;
import com.byyy.ccts.modules.llcx.mapper.LlcxRoleUserMapper;
import com.byyy.ccts.modules.llcx.mapper.LlcxUserMapper;
import com.byyy.ccts.modules.llcx.service.IDanwService;
import com.byyy.ccts.modules.llcx.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @projectName: TODO
 * @package: com.byyy.ccts.modules.llcx.service.impl
 * @className: DanwService
 * @description: TODO
 * @author: LYB
 * @Date: 2023-01-10 11:05
 * @updateUser: LYB
 * @updateDate: 2023-01-10 11:05
 * @updateRemark:
 * @version: v1.0
 */
@Transactional
@Service("danwService")
public class DanwService extends CommonServiceImpl<LlcxDanwMapper, LlcxDanw> implements IDanwService {

    @Autowired
    private LlcxUserMapper llcxUserMapper;

    @Autowired
    private LlcxBaseUserMapper llcxBaseUserMapper;

    @Autowired
    private LlcxRoleUserMapper llcxRoleUserMapper;

    @Override
    public void createDanwUser(List<LlcxDanw> llcxDanwList) {
        LlcxDanw llcxDanwDb = null;
        LlcxUser llcxUser = null;
        LlcxBaseUser llcxBaseUser = null;
        LlcxRoleUser llcxRoleUser = null;
        for (LlcxDanw llcxDanw : llcxDanwList) {
            // 判断是不是存在
            llcxDanwDb = this.selectById(llcxDanw.getDanwId());
            if (llcxDanwDb != null) {
                continue;
            }

            // 不存在就新增
            this.insert(llcxDanw);
            llcxUser = new LlcxUser();
            llcxUser.setId(llcxDanw.getDanwId());
            this.llcxUserMapper.insert(llcxUser);
            llcxBaseUser = new LlcxBaseUser();
            llcxBaseUser.setId(llcxDanw.getDanwId());
            llcxBaseUser.setRealname(llcxDanw.getDanwName());
            llcxBaseUser.setUsername(llcxDanw.getDanwId());
            llcxBaseUser.setPassword(PasswordUtil.encrypt(llcxDanw.getDanwId(), "123456", PasswordUtil.getStaticSalt()));
            this.llcxBaseUserMapper.insert(llcxBaseUser);
            llcxRoleUser = new LlcxRoleUser();
            llcxRoleUser.setId(llcxDanw.getDanwId());
            llcxRoleUser.setUserid(llcxDanw.getDanwId());
            this.llcxRoleUserMapper.insert(llcxRoleUser);
        }
    }

}
